﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeDuce.Model;
using SeDuce.Utility;
using System.IO;
using SeDuce.BLL;

public partial class Management_News_Add : PageBase
{
    List<News> news = new List<News>();

    Lazy_Yu<NewsBLL> lazyNewsBLL = new Lazy_Yu<NewsBLL>(() => new NewsBLL());
    public NewsBLL LazyNewsBLL
    {
        get { return this.lazyNewsBLL.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.DropDownListBind(this.ddlNewsTypes, this.LazyNewsBLL.GetNewsTypes(), "TypeName", "ID");
        }
    }

    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        if (Session["file_info"] != null)
        {
            List<string> file_list_to_delete = new List<string>();
            try
            {
                List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;
                ImageHelp imgHelper = new ImageHelp();

                string UploadPath = Server.MapPath(Misc.news_filepath);
                if (!System.IO.Directory.Exists(UploadPath))
                {
                    System.IO.Directory.CreateDirectory(UploadPath);
                }

                foreach (Thumbnail img in thumbnails)
                {
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Guid.NewGuid().ToString().Replace("-", "");
                    string fullfilename = filename + Path.GetExtension(img.ID).ToLower();
                    file_list_to_delete.Add(Misc.news_filepath + "/" + fullfilename);
                    FileStream fs = new FileStream(UploadPath + "/" + fullfilename, FileMode.Create);
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(img.Data);
                    bw.Close();
                    fs.Close();

                    //watermark
                    //Utils.Watermark(UploadPath + "/Temp_" + fullfilename, UploadPath + "/" + fullfilename, Server.MapPath(Misc.watermark), "FULL");

                    news.Add(new News()
                    {
                        Title = this.txtName.Text.Trim(),
                        ContentText = this.txtContent.Value.Trim(),
                        Pic = Misc.news_filepath + "/" + fullfilename,
                        UpdateDate = DateTime.Now,
                        TypeID = this.ddlNewsTypes.SelectedValue.ToInt(),
                        Seq = this.ddlSeq.SelectedValue.ToInt(),
                        IsInGallery = this.chkIsInGallery.Checked,
                        IsHeadline = this.ddlIsHeadline.SelectedValue.ToInt()
                    });
                }
                if (this.LazyNewsBLL.InsertNews(news[0]))
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key1", "javascript:" + string.Format("alert('{0}');window.location.reload();", "操作成功!"), true);
                }
                else
                {
                    throw new Exception("保存时发生错误");
                }
            }
            catch (Exception ex)
            {
                foreach (string file in file_list_to_delete)
                {
                    if (File.Exists(Server.MapPath(file)))
                    {
                        File.Delete(Server.MapPath(file));
                    }
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key2", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
            }
            finally
            {
                Session.Remove("file_info");
            }
        }
    }
    protected void lbtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("List.aspx");
    }
}